package com.baomidou.ant.controller;

import com.baomidou.ant.common.Result;
import com.baomidou.ant.entity.*;
import com.baomidou.ant.kingdee.service.KingdeeService;
import com.baomidou.ant.service.IPinLibraryService;
import com.baomidou.ant.service.IProcessConditionService;
import com.baomidou.ant.service.IProcessInfoService;
import com.baomidou.ant.service.ISysUserService;
import com.baomidou.ant.utils.MyStringUtil;
import com.baomidou.ant.vo.request.RequestAddPinVo;
import com.baomidou.ant.vo.request.RequestDtoVo;
import com.baomidou.ant.vo.request.RequestListPinVo;
import com.baomidou.ant.vo.request.RequestUpdatePinLibraryVo;
import com.baomidou.ant.vo.response.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2022-03-18
 */
@RestController
@RequestMapping("/pin-library")
public class PinLibraryController {
    @Autowired
    private IPinLibraryService pinLibraryService;

    @PostMapping("/save")
    public Result savePin(@RequestBody RequestAddPinVo vo){
        PinLibrary pinLibrary=new PinLibrary();
        BeanUtils.copyProperties(vo,pinLibrary);
        pinLibrary.setProductStatus(true);
        if(ObjectUtils.isNotEmpty(pinLibraryService.queryByPinName(vo.getPinName(),vo.getDeptId()))){
            return Result.getFailure().setMsg("新增失败!</br>该部门已经存在相同产品");
        }
        if(pinLibraryService.save(pinLibrary)){
            return Result.getSuccess().setMsg("新增成功");
        }else
            return Result.getFailure().setMsg("新增失败");
    }

    //分页查询
    @PostMapping("/listPin")
    public Result listPin(@RequestBody RequestListPinVo vo){
        IPage<ResponseListPinVo> iPage=pinLibraryService.listPin(vo);
        return Result.getSuccess().setData(iPage);
    }

    //下拉框展示各品名
    @PostMapping("/listPinName")
    public Result listPinName(@RequestBody RequestDtoVo requestDtoVo){
        QueryWrapper<PinLibrary> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("dept_id",requestDtoVo.getDeptId());
        queryWrapper.eq("product_status",1);
        List<PinLibrary> pinList=pinLibraryService.list(queryWrapper);
        List<ResponseListPinNameVo> list=new ArrayList<>();
        for(PinLibrary pinLibrary:pinList){
            ResponseListPinNameVo vo=new ResponseListPinNameVo();
            BeanUtils.copyProperties(pinLibrary ,vo);
            list.add(vo);
        }
        return Result.getSuccess().setData(list);
    }

    //下拉框展示各产品类型
    @GetMapping("/listProductType/{pinId}")
    public Result listProductType(@PathVariable String pinId){
        ResponseListTypeProcessVo vo=new ResponseListTypeProcessVo();
        if(Objects.equals(pinId,"null")){
            vo.setProductType("无对应产品");
            return Result.getSuccess().setData(vo);
        }else {
            PinLibrary pinLibrary=pinLibraryService.getById(pinId);
            vo.setProductType(pinLibrary.getProductType());
            vo.setProcess(pinLibrary.getProcess());
            return Result.getSuccess().setData(vo);
        }
    }

    //查看相关工序接口
    @GetMapping("/listProcessInfo/{pinId}")
    public Result listProcessInfo(@PathVariable Integer pinId){
        ResponseListProcessInfoVo vo=pinLibraryService.listProcessInfo(pinId);
        return Result.getSuccess().setData(vo);
    }

    //获取产品信息接口(编辑时)
    @GetMapping("/getPinInfo/{pinId}")
    public Result getPinInfo(@PathVariable Integer pinId){
        PinLibrary pinLibrary=pinLibraryService.getById(pinId);
        ResponseGetPinInfoVo vo=new ResponseGetPinInfoVo();
        BeanUtils.copyProperties(pinLibrary,vo);
        return Result.getSuccess().setData(vo);
    }

    //修改接口
    @PostMapping("/update")
    public Result update(@RequestBody RequestUpdatePinLibraryVo vo){
        PinLibrary pinLibrary=pinLibraryService.getById(vo.getId());
        BeanUtils.copyProperties(vo,pinLibrary);
        pinLibrary.setProductStatus(Objects.equals(vo.getStatus(), "1"));
        if(pinLibraryService.updateById(pinLibrary)){
            return Result.getSuccess().setMsg("保存成功");
        }else
            return Result.getFailure().setMsg("保存失败");
    }

    @PostMapping("/queryByKingdee")
    public Result queryByKingdee(){
        return Result.getSuccess().setData(KingdeeService.queryMaterial());
    }


}
